package com.lezheba.db;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

public class Dao {
	static Logger logger = Logger.getLogger(Dao.class.getName());

	private static Dao dao;

	public static Dao getInstance() {
		if (dao == null) {
			dao = new Dao();
		}
		return dao;
	}

	public void save(Object object) {
		try {
			getSession().save(object);
		} catch (HibernateException e) {
			logger.error("HibernateException: " + e.toString());
		}
	}

	public void update(Object object) {
		try {
			getSession().update(object);
		} catch (HibernateException e) {
			logger.error("HibernateException: " + e.toString());
		}
	}

	public void saveOrUpdate(Object object) {
		try {
			getSession().saveOrUpdate(object);
		} catch (HibernateException e) {
			logger.error("HibernateException: " + e.toString());
		}
	}

	@SuppressWarnings("rawtypes")
	public List getListByHql(String hql, Object[] paras) {
		Session session = getSession();
		Query query = session.createQuery(hql);
		if (paras != null) {
			for (int i = 0; i < paras.length; i++) {
				query.setParameter(i, paras[i]);
			}
		}
		List list = query.list();
		closeSession();
		return list;
	}
	
	@SuppressWarnings("rawtypes")
	public List getListByHql(String hql) {
		return getListByHql(hql, null);
	}
	
	@SuppressWarnings("rawtypes")
	public List getListByHqlPage(String hql, Object[] paras, int pageNum, int pageSize) {
		Session session = getSession();
		Query query = session.createQuery(hql);
		if (paras != null) {
			for (int i = 0; i < paras.length; i++) {
				query.setParameter(i, paras[i]);
			}
		}
		query.setFirstResult((pageNum - 1) * pageSize);  
	    query.setMaxResults(pageSize);  
		List list = query.list();
		closeSession();
		return list;  
	}

	@SuppressWarnings("rawtypes")
	public List getList(String sql, Object[] paras) {
		Session session = getSession();
		SQLQuery query =session.createSQLQuery(sql);
		for (int i = 0; i < paras.length; i++) {
			query.setParameter(i, paras[i]);
		}
		List list = query.list();
		closeSession();
		return list;  
	}

	@SuppressWarnings("rawtypes")
	public Object getObject(String sql, Object[] paras) {
		List list = getList(sql, paras);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public Object getObjectByHql(String hql, Object[] paras) {
		List list = getListByHql(hql, paras);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public List getListByHql(String hql, Object para) {
		return getListByHql(hql, new Object[] { para });
	}

	@SuppressWarnings("rawtypes")
	public List getList(String sql, Object para) {
		return getList(sql, new Object[] { para });
	}

	public Object getObject(String sql, Object para) {
		return getObject(sql, new Object[] { para });
	}

	public Object getObjectByHql(String hql, Object para) {
		return getObjectByHql(hql, new Object[] { para });
	}


	protected Session getSession(){
		return HibernateUtil.getSession();
	}
	
	protected void closeSession(){
		HibernateUtil.closeSession();
	}


}
